#include "teldataDJ.h"
#include "generateData.h"
#include <iomanip>
#include <iostream>
#include <codecvt>
#include <thread>
#include <chrono>
#include <shared_mutex>
#include <mutex>
#include <random>

using namespace std;

ParseTelDataDJ::ParseTelDataDJ()
{
    // 初始化监测数据发送
    string targetIP_tele1 = "192.168.1.51";
    int port_tele1 = 6075;
    Sendteledata1.setip_add(port_tele1, (char*)targetIP_tele1.data());

    int port_tele_multi = 10001;
    string targetIP_tele_multi = "227.0.200.1";
    Sendteledata_multi.setip_add_multi(port_tele_multi, (char*)targetIP_tele_multi.data());

    int port_tele_multi1 = 10002;
    string targetIP_tele_multi1 = "227.0.200.1";
    Sendteledata_multi1.setip_add_multi(port_tele_multi1, (char*)targetIP_tele_multi1.data());

    int port_tele_multi2 = 10003;
    string targetIP_tele_multi2 = "227.0.200.1";
    Sendteledata_multi2.setip_add_multi(port_tele_multi2, (char*)targetIP_tele_multi2.data());
}

ParseTelDataDJ::~ParseTelDataDJ()
{
}

void ParseTelDataDJ::run()
{
    char buf_dj_tele[2048];
    int num_DjData;

    int count_all = 0;
    auto start = std::chrono::system_clock::now();

    while (1)
    {
        auto TaskData = generateTaskData();
//        Sendteledata_multi.sendData(TaskData.data(), sizeof(TaskData.data()));
        Sendteledata_multi.sendData(TaskData.data(), TaskData.size());
//        std::cout << "==================================================="<< std::endl;
//        std::cout << "sizeof(TaskData.data(): "
//                  << sizeof(TaskData.data())<< std::endl;
//        std::cout << "TaskData.size(): "
//                  << TaskData.size()<< std::endl;
//        std::cout << "==================================================="<< std::endl;

        auto waypointData = generateWaypointData();
        Sendteledata_multi1.sendData(waypointData.data(), waypointData.size());//
//        std::cout << "==================================================="<< std::endl;
//        std::cout << "sizeof(waypointData.data(): "
//                  << sizeof(waypointData.data())<< std::endl;
//        std::cout << "waypointData.size(): "
//                  << waypointData.size()<< std::endl;
//        std::cout << "==================================================="<< std::endl;


        auto TargetData = generateTargetData();
        Sendteledata_multi2.sendData(TargetData.data(), TargetData.size());
//        std::cout << "sizeof(TargetData.data(): "
//                  << sizeof(TargetData.data())<< std::endl;
//        std::cout << "TargetData.size(): "
//                  << TargetData.size()<< std::endl;
//        std::cout << "==================================================="<< std::endl;


        // 视频发送过程中小数据发送模拟测试//灌包测试
        count_all = count_all + 1;
        std::cout << "count_all : " << count_all << std::endl;

        auto now = std::chrono::system_clock::now();
        auto durtion = now - start;
        auto mill = std::chrono::duration_cast<std::chrono::milliseconds>(durtion);
        std::cout << "Time cost : " << mill.count() << std::endl;

        // 发送速率调节//延迟//帧率50//帧率100
        // std::this_thread::sleep_for(std::chrono::milliseconds(20));
        // std::this_thread::sleep_for(std::chrono::microseconds(20000)); //约50Hz
        std::this_thread::sleep_for(std::chrono::microseconds(10));
    }
}

